package com.android.ClientApp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NetworkOptions extends Activity implements OnClickListener {
    private EditText serverIPText;
    private EditText serverPortText;
    
    private static String server_ip = new String("129.16.184.93");
    private static int server_port = 5163;
    
    // Booleans to determine which network type is being used
    private boolean bluetooth = false;
    private boolean tcp = false;
    
    // Regular expression patterns to check if an IP-ADDRESS is valid
    private Pattern pattern;
    private Matcher matcher;
    
    private static final String IPADDRESS_PATTERN = 
		"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
		"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
		"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
		"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";    
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_options);
		
        Button setIPButton = (Button)findViewById(R.id.setIPButton);
        setIPButton.setOnClickListener(this);
               
        Button setPortButton = (Button)findViewById(R.id.setPortButton);
        setPortButton.setOnClickListener(this);
        
        Button connectButton = (Button)findViewById(R.id.connectButton);
        connectButton.setOnClickListener(this);
        
        Button disconnectButton = (Button)findViewById(R.id.disconnectButton);
        disconnectButton.setOnClickListener(this);
        
        serverIPText = (EditText)findViewById(R.id.serverIPText);
        serverPortText = (EditText)findViewById(R.id.serverPortText);
        
        pattern = Pattern.compile(IPADDRESS_PATTERN);
	}
	
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.connectButton:
				if(bluetooth) {
					// TODO
				}
				if(tcp) {
					NetworkHandling.establishConnection(server_ip,server_port);
				}
			break;
			
		case R.id.disconnectButton:
				if(bluetooth) {
					// TODO
				}
				if(tcp) {
					NetworkHandling.disconnect();
				}
			break;
			
		case R.id.setIPButton:
				String ip = serverIPText.getText().toString();
				matcher = pattern.matcher(ip);
			
				if(matcher.matches()) {
					server_ip = ip;
					createToastMsg("The server IP is now set to " + server_ip);
				}
				else {
					createToastMsg("Not a valid IP-ADDRESS, using default IP");
				}
			break;
		
		case R.id.setPortButton:
				int port = Integer.parseInt(serverPortText.getText().toString());
				if(port >= 1024 && port <= 65535) {
					server_port = port;
					createToastMsg("The server port is now set to " + server_port);
				}
				else {
					createToastMsg("Not a valid port, 1025 <= port <= 65535");
				}
			break;
		}
		
	}
	
	public void onRadioButtonClicked(View v) {
		switch(v.getId()) {
		case R.id.radio_bluetooth:
				bluetooth = true;
				tcp = false;
				
				// Do more stuff
			break;
			
		case R.id.radio_tcp:
				bluetooth = false;
				tcp = true;
			break;
		}
	}
	
	protected void createToastMsg(String textmsg) {
		Toast m = Toast.makeText(getApplicationContext(), textmsg, 3500);
		m.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
		m.show();
	}

}
